package com.mashibing.service.impl.redisImpl;

import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.request.DriverGrabRequest;
import com.mashibing.service.GrabService;
import com.mashibing.service.OrderService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service("grabByRedissonSentinelYmlService")
public class GrabByRedissonSentinelYmlServiceImpl implements GrabService {

    @Autowired
    private OrderService orderService;

    @Autowired
    @Qualifier("redissonSentinelYmlClient")
    private RedissonClient redissonClient;


    @Override
    public ResponseResult grab(DriverGrabRequest driverGrabRequest) {

        String orderIdKey = driverGrabRequest.getOrderId() + "";

        System.out.println("开始锁 redissonSentinelYml");
        RLock lock = redissonClient.getLock(orderIdKey);
        lock.lock();

        // 模拟实际情况测试代码
//        try {
//            TimeUnit.SECONDS.sleep(10);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        ResponseResult responseResult = orderService.driverGrab(driverGrabRequest);
        lock.unlock();
        System.out.println("结束锁 redissonSentinelYml");

        return responseResult;
    }


}
